package goleetcode

import "strconv"

func calPoints(operations []string) int {
	ans := 0
	res := make([]int, 0)
	for _, op := range operations {
		n := len(res)
		switch op[0] {
		case '+':
			ans += res[n-1] + res[n-2]
			res =
				append(res, res[n-1]+res[n-2])
		case 'D':
			ans += res[n-1] * 2
			res = append(res, res[n-1]*2)
		case 'C':
			ans -= res[n-1]
			res = res[:len(res)-1]
		default:
			pt, _ := strconv.Atoi(op)
			ans += pt
			res = append(res, pt)
		}
	}
	return ans
}
